//
//  AppDelegate.swift
//  cxd4iphone
//
//  Created by hexy on 15/11/19.
//  Copyright © 2015年 hexy. All rights reserved.
//


import UIKit
import AFNetworking

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        window?.backgroundColor = UIColor.whiteColor()
        
        window?.rootViewController = defaultRootViewController()
        window?.makeKeyAndVisible()
        
        startObserverNotification()
        makeNetwork()
        startBugTags()
        startBaiduMobStat()

        return true
    }
    
    func applicationWillResignActive(application: UIApplication) {

    }

    func applicationDidEnterBackground(application: UIApplication) {

    }

    func applicationWillEnterForeground(application: UIApplication) {

    }

    func applicationDidBecomeActive(application: UIApplication) {

    }

    func applicationWillTerminate(application: UIApplication) {

    }
}

extension AppDelegate {
    
    private func startObserverNotification() {
        
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "chooseRootViewController:", name: Define.chooseRootViewControllerNotification, object: nil)
    }
    
    @objc private func chooseRootViewController(noti: NSNotification) {
        
        xx_print(noti.object)
        window?.rootViewController = MainViewController()
    }

    private func makeNetwork() {
        
        AFNetworkActivityIndicatorManager.sharedManager().enabled = true
        
        let cache = NSURLCache(memoryCapacity: 4 * 1024 * 1024, diskCapacity: 20 * 1024 * 1024, diskPath: nil)
        NSURLCache.setSharedURLCache(cache)
    }
    
    private func startBugTags() {
        
        Bugtags.startWithAppKey("210c538ffdff41728009c352edd475b4", invocationEvent: BTGInvocationEventShake)
    }
    
    private func startBaiduMobStat() {
        
        let statTracker = BaiduMobStat.defaultStat()
        
        statTracker.shortAppVersion = xx_version()
        statTracker.enableExceptionLog = false
        statTracker.monitorStrategy = BaiduMobStatMonitorStrategyPageView
        statTracker.enableDebugOn = false
        
        statTracker.startWithAppId("cf69ff8877")
    }
    
    private func isNewVersion() -> Bool {
        
        let bundleVersion = (xx_version() as NSString).doubleValue
        
        let versionKey = "versionKey"
        let saxboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(versionKey)
        
        NSUserDefaults.standardUserDefaults().setDouble(bundleVersion, forKey: versionKey)
        
        return bundleVersion > saxboxVersion
    }
    
    private func defaultRootViewController() -> UIViewController {
        
        return isNewVersion() ? NewFeatureViewController() : MainViewController()
    }
    
    class func selectMeViewController() {
        
        let delegate = UIApplication.sharedApplication().delegate as! AppDelegate
        
        let main = delegate.window?.rootViewController as! MainViewController
        
        main.selectedIndex = 3
    }
}

